package com.helen.ui.account;

import android.content.Context;

import com.helen.db.UserHelper;
import com.helen.global.Constants;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

/**
 * Created by yang on 2018/4/2.
 * 获取用户金宝和余额
 */

public class RefreshMoneyUtils {
    private OnResultListener resultListener;

    public void setOnResultListener(OnResultListener listener) {
        this.resultListener = listener;
    }

    public interface OnResultListener {
        void onResult(double money,int jinbao);
    }


    public void refreshMoneyApi(final Context mContext) {
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(mContext).getId());
        EasyHttp.post(Constants.API_JSONAPI_REFRESHMONEY)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        MyLog.d("yang", "刷新用户账号失败==" + e.toString());
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "刷新用户账号成功s==---------" + result);
                        if (result != null) {
                            int code = JsonUtil.getJsonValuesInt(result, "code");
                            String msg = JsonUtil.getJsonValuesString(result, "msg");
                            if (code == 0) {
                                double money = JsonUtil.getJsonValuesDouble(result, "money");
                                int jibao = JsonUtil.getJsonValuesInt(result, "jinbao");
                                if (resultListener != null) {
                                    resultListener.onResult(money,jibao);
                                }
                            } else {
                                MToast.makeTextLong(mContext, msg).show();
                            }


                        }

                    }
                });


    }
}
